/*
* @(#)RootRetrievalManager.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.dms.core;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.dcivision.dms.DmsOperationConstant;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsLocMaster;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.dms.dao.DmsDocumentDAObject;
import com.dcivision.dms.dao.DmsLocMasterDAObject;
import com.dcivision.dms.dao.DmsRootDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.PermissionManager;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.Utility;

/**
 * <p>Class Name:       RootRetrievalManager.java    </p>
 * <p>Description:      The class mainly handle the root of dmsDocument list's process and reterieval operations.</p>
 *
 * @author              Zoe Shum
 * @company             DCIVision Limited
 * @creation date       01/08/2003
 * @version             $Revision: 1.32 $
 */

public class RootRetrievalManager {

  public static final String REVISION = "$Revision: 1.32 $";

  private SessionContainer sessionContainer;
  private Connection conn;

  /**
   * Constructor
   *
   * @param sessionContainer
   * @param conn
   */
  public RootRetrievalManager(SessionContainer sessionContainer, Connection conn) {
    this.sessionContainer = sessionContainer;
    this.conn = conn;
  }

  /**
   *  Release - release all variable in instance of DocumentRetrievalManager
   */
  public void release () {
    this.sessionContainer = null;
    this.conn = null;
  }

  /**
   * Gets the DmsLocMaster by Owner ID
   *
   * @param ownerID  User Record ID
   * @return DmsLocMaster
   * @throws ApplicationException
   * @see com.dcivision.dms.bean.DmsLocMaster
   */
  public DmsLocMaster getLocMasterOfPersonalRootByOwnerID(Integer ownerID) throws ApplicationException {
    DmsLocMasterDAObject locMasterDAO = new DmsLocMasterDAObject(sessionContainer, conn);
    DmsRootDAObject dmsRootDAO = new DmsRootDAObject(sessionContainer, conn);
    DmsRoot dmsRoot = dmsRootDAO.getPersonalRootByOwnerIDAndType(ownerID, GlobalConstant.RECORD_STATUS_ACTIVE);
    DmsLocMaster locMaster = null;
    if (dmsRoot != null) {
      locMaster = (DmsLocMaster) locMasterDAO.getObjectByID(dmsRoot.getLocID());
    }
    locMasterDAO = null;
    dmsRootDAO = null;
    return locMaster;
  }

  /**
   * Gets the DmsLocMaster by dmsDocument object
   *
   * @param dmsDocument dmsDocument object
   * @return DmsLocMaster
   * @throws ApplicationException
   * @see com.dcivision.dms.bean.DmsLocMaster
   */
  public DmsLocMaster getTargetLocMasterByDocument(DmsDocument dmsDocument) throws ApplicationException {
    DmsLocMasterDAObject locMasterDAO = new DmsLocMasterDAObject(sessionContainer, conn);
    DmsRootDAObject dmsRootDAO = new DmsRootDAObject(sessionContainer, conn);
    DmsRoot dmsRoot = (DmsRoot) dmsRootDAO.getObjectByIDIgnoreRecordStatus(dmsDocument.getRootID());
    DmsLocMaster locMaster = (DmsLocMaster) locMasterDAO.getObjectByID(dmsRoot.getLocID());
    locMasterDAO=null;
    dmsRootDAO=null;
    return locMaster;
  }

  /**
   * Gets the DmsLocMaster by DmsLocMaster ID
   *
   * @param locID the ID of dmsLocMaster
   * @return DmsLocMaster
   * @throws ApplicationException
   */
  public DmsLocMaster getTargetLocMasterByLocID(Integer locID) throws ApplicationException {
    DmsLocMasterDAObject locMasterDAO = new DmsLocMasterDAObject(sessionContainer, conn);
    DmsLocMaster locMaster = (DmsLocMaster) locMasterDAO.getObjectByID(locID);
    locMasterDAO=null;
    return locMaster;
  }

  /**
   * Gets the DmsDocument by root ID.
   *
   * @param rootID      The root ID.
   * @return            The DMS document object of the root folder.
   * @throws ApplicationException
   * @see com.dcivision.dms.bean.DmsDocument
   */
  public DmsDocument getRootDocument(Integer rootID) throws ApplicationException {
    DmsDocumentDAObject dmsDocumentDAO = new DmsDocumentDAObject(sessionContainer, conn);
    List resultList = dmsDocumentDAO.getListByParentID(new Integer(0), rootID, DmsDocument.FOLDER_TYPE);
    dmsDocumentDAO=null;
    if (Utility.isEmpty(resultList)) {
      return (null);
    } else {
      return((DmsDocument)resultList.get(0));
    }
  }

  /**
   * Gets the DmsRoot by root ID
   *
   * @param rootID      The root ID.
   * @return            The DmsRoot object of the root folder.
   * @throws ApplicationException
   * @see com.dcivision.dms.bean.DmsRoot
   */
  public DmsRoot getRootObject(Integer rootID) throws ApplicationException {
    DmsRootDAObject dmsRootDAO = new DmsRootDAObject(sessionContainer, conn);
    DmsRoot result = (DmsRoot)dmsRootDAO.getObjectByID(rootID);
    dmsRootDAO=null;
    return result;
  }

  /**
   * Gets all the DmsRoot(s) that current user is accessible
   *
   * @param
   * @return            The List of all accessible public root folders.
   * @throws ApplicationException
   */
  public List getAccessiblePublicRootsList() throws ApplicationException {
    PermissionManager permissionManager = sessionContainer.getPermissionManager();
    List resultList = new ArrayList();
    if (permissionManager.hasAccessRight(SystemFunctionConstant.DMS_PUBLIC_FOLDER, "R")){
      DmsRootDAObject dmsRootDAO = new DmsRootDAObject(sessionContainer, conn);
      resultList = dmsRootDAO.getPublicRootList();
      dmsRootDAO = null;
    }
    return resultList;
  }

  /**
   * Gets all the accessible roots (include personal and public) of current user
   *
   * @param
   * @return            The List of all accessible root folders.
   * @throws ApplicationException
   */
  public List getAllAccessibleRootsList() throws ApplicationException {
    //All accessible public root list
    List resultList = this.getAccessiblePublicRootsList();
    //Personal Root
    DocumentRetrievalManager docRetrevalManager = new DocumentRetrievalManager(sessionContainer, conn);
    RootOperationManager rootOperationManager = new RootOperationManager(sessionContainer, conn);
    PermissionManager permissionManager = sessionContainer.getPermissionManager();
    
    boolean isLightVersion = this.sessionContainer.getAppContainer().isLightVersion();

    Integer userID = sessionContainer.getUserRecordID();
    DmsRootDAObject rootDAO = new DmsRootDAObject(sessionContainer, conn);
    List rootList = rootDAO.getRootByOwnerIDAndType(userID, DmsRoot.PERSONAL_ROOT, GlobalConstant.RECORD_STATUS_ACTIVE);
    DmsRoot rootPoint = null;
    if (rootList.size() == 0) {
      // Create default root for user without root point
      rootPoint = rootOperationManager.createDefaultPersonalRootPointForUser();
    } else {
      rootPoint = (DmsRoot)rootList.get(0);
    }

    DmsDocument personalFolder = docRetrevalManager.getDocumentByNameParentIDCreatorID(DmsOperationConstant.DMS_PERSONAL_ROOT, new Integer(0),
      sessionContainer.getUserRecordID());
    DmsRoot personalRoot = this.getRootObjectByOwnerID(sessionContainer.getUserRecordID());

    if (personalRoot != null) {
      personalRoot.setRootFolderID(personalFolder.getID());

      String sFunctionCode = null;
      
      /** Not add personal root checking in lite version */
      if(!isLightVersion){
        sFunctionCode = this.sessionContainer.getAppContainer().getSysFunctionByFunctionID(new Integer(6001)).getFunctionCode();
        if (permissionManager.hasAccessRight(sFunctionCode, "R")) {
          resultList.add(0, personalRoot);
        }
      }



    }
    docRetrevalManager.release();
    rootOperationManager.release();

    return resultList;
  }
  
  /**
   * Gets all the personal accessible roots of current user
   *
   * @param
   * @return            The List of personal accessible root folders.
   * @throws ApplicationException
   */
  public List getAccessiblePersonalRootsList() throws ApplicationException {
    List lstResultList = new ArrayList();
    //Personal Root
    DocumentRetrievalManager docRetrevalManager = new DocumentRetrievalManager(sessionContainer, conn);
    RootOperationManager rootOperationManager = new RootOperationManager(sessionContainer, conn);
    PermissionManager permissionManager = sessionContainer.getPermissionManager();
    
    boolean isLightVersion = this.sessionContainer.getAppContainer().isLightVersion();

    Integer userID = sessionContainer.getUserRecordID();
    DmsRootDAObject rootDAO = new DmsRootDAObject(sessionContainer, conn);
    List rootList = rootDAO.getRootByOwnerIDAndType(userID, DmsRoot.PERSONAL_ROOT, GlobalConstant.RECORD_STATUS_ACTIVE);
    DmsRoot rootPoint = null;
    if (rootList.size() == 0) {
      // Create default root for user without root point
      rootPoint = rootOperationManager.createDefaultPersonalRootPointForUser();
    } else {
      rootPoint = (DmsRoot)rootList.get(0);
    }

    DmsDocument personalFolder = docRetrevalManager.getDocumentByNameParentIDCreatorID(DmsOperationConstant.DMS_PERSONAL_ROOT, new Integer(0),
      sessionContainer.getUserRecordID());
    DmsRoot personalRoot = this.getRootObjectByOwnerID(sessionContainer.getUserRecordID());

    if (personalRoot != null) {
      personalRoot.setRootFolderID(personalFolder.getID());

      String sFunctionCode = null;
      
      /** Not add personal root checking in lite version */
      if(!isLightVersion){
        sFunctionCode = this.sessionContainer.getAppContainer().getSysFunctionByFunctionID(new Integer(6001)).getFunctionCode();
        if (permissionManager.hasAccessRight(sFunctionCode, "R")) {
          lstResultList.add(0, personalRoot);
        }
      }



    }
    docRetrevalManager.release();
    rootOperationManager.release();

    return lstResultList;
  }

  /**
   * Gets personal root of specified user
   *
   * @param      ownerID   User record ID 
   * @return     The target DmsRoot object for the input owner
   * @throws ApplicationException
   */
  public DmsRoot getRootObjectByOwnerID(Integer ownerID) throws ApplicationException {
    DmsRootDAObject dmsRootDAO = new DmsRootDAObject(sessionContainer, conn);
    DmsRoot dmsRoot = dmsRootDAO.getPersonalRootByOwnerIDAndType(ownerID, GlobalConstant.RECORD_STATUS_ACTIVE);
    dmsRootDAO=null;
    return dmsRoot;
  }

  /**
   * Gets the space used under the root
   *
   * @param rootID  the root ID
   * @return Double the space used in bytes
   * @throws ApplicationException
   */
  public Double getUsedSpace(Integer rootID) throws ApplicationException {
    DmsRootDAObject dmsRootDAO = new DmsRootDAObject(sessionContainer, conn);
    Double space = dmsRootDAO.getUsedSpaceByRootID(rootID);
    dmsRootDAO = null;
    return (space);
  }
}
